//
//  MySettingController.m
//  LoveFreshBeen
//
//  Created by 魏根 on 2016/10/17.
//  Copyright © 2016年 魏根. All rights reserved.
//

#import "MySettingController.h"
#import "AboutMeController.h"
#import <SDImageCache.h>

static NSString *reuseID = @"id";



@interface MySettingController ()

@end

@implementation MySettingController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self setHeaderNAV];
}

- (BOOL)prefersStatusBarHidden{
    
    return YES;
}

- (void)setHeaderNAV{
    
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:reuseID];
    
    self.tableView.tableFooterView = [[UIView alloc]init];
    
    self.tableView.bounces = NO;
    
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"grey1"]];
    
     self.tableView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1.0];
    //头view
    UIView *myNAV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 80)];
    
    self.tableView.tableHeaderView = myNAV;
    
    myNAV.backgroundColor = [UIColor whiteColor];
    //中间的设置
    UILabel *title = [[UILabel alloc]init];
    
    [myNAV addSubview:title];
    
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.equalTo(myNAV);
        
    }];
    
    title.text = @"设置";
    
    title.font = [UIFont systemFontOfSize:20];
    //左边的返回按钮
    UIButton *backBtn = [[UIButton alloc]init];
    
    [backBtn setBackgroundImage:[UIImage imageNamed:@"v2_goback"] forState:UIControlStateNormal];
    
    [myNAV addSubview:backBtn];
    
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(myNAV).offset(10);
        
        make.centerY.equalTo(myNAV);
    }];
    
    [backBtn addTarget:self action:@selector(backBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)backBtnDidClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

//隐藏 tabBar
- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    [self.tabBarController.tabBar setHidden:NO];
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self.tabBarController.tabBar setHidden:YES];
    
    [self.tableView reloadData];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if(section == 0){
        
        return 2;
    }
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID forIndexPath:indexPath];
    
    if (indexPath.section == 1 && indexPath.row == 0) {
        
        cell.textLabel.text = @"退出当前账号";
        
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        cell.textLabel.text = @"关于小熊";
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    if (indexPath.section == 0 && indexPath.row == 1) {
        
        cell.textLabel.text = @"清理缓存";
        
        UILabel *cacheBulk = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
        
       NSString *cachePath =  [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
        
        CGFloat fileSize = [self folderSizeAtPath:cachePath];
        
        cacheBulk.text = [NSString stringWithFormat:@"%.2f M",fileSize];
        
        cacheBulk.textAlignment = NSTextAlignmentRight;
        
        cell.accessoryView = cacheBulk;
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 20;
    }
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *cachePath =  [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        AboutMeController *aboutVC = [[AboutMeController alloc]init];
       
        [self.navigationController pushViewController:aboutVC animated:YES];
    }
    
    if (indexPath.section == 0 && indexPath.row == 1) {
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
            [self clearCache:cachePath];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                  [self.tableView reloadData];
            });
        });
        
        
        
      
    }
    
    if (indexPath.section == 1 && indexPath.row == 0) {
        
//        [SVProgressHUD showWithStatus:@"确认退出"];
        
        [SVProgressHUD showInfoWithStatus:@"确认退出"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD dismiss];
            
            [self.tableView reloadData];
        });
    }
    
    
}
// 计算单个文件大小
- (float)fileSizeAtPath:(NSString *)path{
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if ([fileManager fileExistsAtPath:path]) {
        
        long long size = [fileManager attributesOfItemAtPath:path error:nil].fileSize;
        
        return size / 1024.0 / 1024.0;
    }
    return 0;
}

//计算目录大小

- (float)folderSizeAtPath:(NSString *)path{
    
    NSFileManager *fileManager = [NSFileManager defaultManager];

    float folderSize = 0.0;
    
    if ([fileManager fileExistsAtPath:path]){
        
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        
        
        for(NSString *fileName in childerFiles) {
            
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            
            folderSize += [self fileSizeAtPath:absolutePath];
        }
        
        folderSize += [[SDImageCache sharedImageCache]getSize] / 1024.0 / 1024.0;
        
        return folderSize;
    }
    
    return 0;
}

//清理缓存文件

- (void)clearCache:(NSString *)path{

    NSFileManager *fileManage = [NSFileManager defaultManager];

    if ([fileManage fileExistsAtPath:path]){

        NSArray *childerFiles = [fileManage subpathsAtPath:path];
        
        
        for(NSString *fileName in childerFiles) {

            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            
            [fileManage removeItemAtPath:absolutePath error:nil];
        }
    }

    [[SDImageCache sharedImageCache]cleanDisk];
}












@end
